<template>
  <div class="logistics">
    <div style="padding-left: 20px;">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['logistics:logistics:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['logistics:logistics:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['logistics:logistics:remove']">删除</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="logisticsList" @selection-change="handleSelectionChange" :height="tableHeight" custom-class="show-back-class">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="物流类型" align="center" prop="logisticsForm" >
          <template slot-scope="scope">
            {{selectDictLabel($store.state.referenceDict.logisticsForm, scope.row.logisticsForm)}}
          </template>
        </el-table-column>
        <el-table-column label="物流基于类型" align="center" prop="logisticsType" >
          <template slot-scope="scope">
            {{selectDictLabel($store.state.referenceDict.logisticsType, scope.row.logisticsType)}}
          </template>
        </el-table-column>
        <el-table-column label="物流承担方" align="center" prop="logisticsProvider" />
        <el-table-column label="运输工具所有权和控制权" align="center" prop="vehicleOwnershipAndControl" >
          <template slot-scope="scope">
            {{selectDictLabel($store.state.referenceDict.vehicleOwnershipAndControl, scope.row.vehicleOwnershipAndControl)}}
          </template>
        </el-table-column>
        <el-table-column label="燃料类型和单位" align="center" prop="fuelAndUnits">
          <template slot-scope="scope">
            {{selectDictLabel(referenceDataList, scope.row.fuelAndUnits,'id','fuel')}}
            <span v-if="scope.row.technologyAndRegion">-</span>
            {{selectDictLabel(referenceDataList, scope.row.fuelAndUnits,'id','unit')}}
          </template>
        </el-table-column>
        <el-table-column label="年消耗量" align="center" prop="quantityPerYear" />
        <el-table-column label="方式和描述" align="center" prop="modeAndDescription">
          <template slot-scope="scope" v-if="scope.row.logisticsForm == 1">
            {{selectDictLabel(referenceDataList, scope.row.modeAndDescription,'id','mode')}}
            <span v-if="scope.row.technologyAndRegion">-</span>
            {{selectDictLabel(referenceDataList, scope.row.modeAndDescription,'id','type')}}
          </template>
        </el-table-column>
        <el-table-column label="每年吨千米" align="center" prop="tonKmPerYear">
          <template slot-scope="scope" v-if="scope.row.logisticsForm == 1">
            {{ scope.row.tonKmPerYear }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['logistics:logistics:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['logistics:logistics:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    </div>

    <!-- 添加或修改物流对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body custom-class="show-back-class">
      <el-form ref="form" :model="form" :rules="rules" label-width="170px">
        <el-form-item label="物流类型 " prop="logisticsForm">
          <el-select v-model="form.logisticsForm" placeholder="请选择物流类型 ">
            <el-option class="index-option" v-for="dict in  $store.state.referenceDict.logisticsForm" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="parseInt(dict.dictValue)" />
          </el-select>
        </el-form-item>

          <el-form-item label="物流基于类型 " prop="logisticsType">
            <el-select v-model="form.logisticsType" placeholder="请选择物流基于类型 ">
              <el-option class="index-option" v-for="dict in  $store.state.referenceDict.logisticsType" :key="dict.dictValue"
                         :label="dict.dictLabel" :value="parseInt(dict.dictValue)" />
            </el-select>
          </el-form-item>
        <el-form-item label="物流承担方" prop="logisticsProvider">
          <el-input v-model="form.logisticsProvider" placeholder="请输入物流承担方" />
        </el-form-item>

        <el-form-item label="运输工具所有权和控制权 " prop="vehicleOwnershipAndControl">
          <el-select v-model="form.vehicleOwnershipAndControl" placeholder="请选择物流基于类型 ">
            <el-option class="index-option" v-for="dict in  $store.state.referenceDict.vehicleOwnershipAndControl" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="parseInt(dict.dictValue)" />
          </el-select>
        </el-form-item>

          <el-row v-if=" form.logisticsForm===1 || form.logisticsForm===2 && form.logisticsType===1" >
            <el-form-item label="燃料类型和单位" prop="fuelAndUnits" >
              <el-select v-model="form.fuelAndUnits" placeholder="请选择燃料类型和单位" style="width: 100%"  >
                <el-option class="index-option" v-for="dict in  referenceDataList" :key="dict.id" v-if="dict.referenceType===2"
                           :label="dict.fuel + ' - ' + dict.unit" :value="parseInt(dict.id)" />
              </el-select>
            </el-form-item>
            <el-form-item label="年消耗量" prop="quantityPerYear">
              <el-input v-model="form.quantityPerYear" placeholder="请输入年消耗量" />
            </el-form-item>
          </el-row>
            <el-row v-if=" form.logisticsForm===1 || form.logisticsForm===2 && form.logisticsType===2" >
            <el-form-item label="方式和描述" prop="modeAndDescription" >
              <el-select v-model="form.modeAndDescription" placeholder="请选择方式和描述" style="width: 100%"  >
                <el-option class="index-option" v-for="dict in  referenceDataList" :key="dict.id" v-if="dict.referenceType===7"
                           :label="dict.mode + ' - ' + dict.type" :value="parseInt(dict.id)" />
              </el-select>
            </el-form-item>
            <el-form-item label="每年吨千米" prop="tonKmPerYear">
              <el-input v-model="form.tonKmPerYear" placeholder="请输入每年吨千米" />
            </el-form-item>
          </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLogistics, getLogistics, delLogistics, addLogistics, updateLogistics, exportLogistics } from "@/api/gasEmission/logistics";
import { referenceDataDict,listReferenceData} from "@/api/reference/referenceData";
export default {
  name: "Logistics",
  props:{
    enterpriseId:{
      type: Number,
      required:true,
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流表格数据
      logisticsList: [],
      referenceDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        logisticsType: null,
        logisticsForm: null,
        logisticsProvider: null,
        vehicleOwnershipAndControl: null,
        fuelAndUnits: null,
        quantityPerYear: null,
        modeAndDescription: null,
        tonKmPerYear: null,
        referenceId: null,
        infoId: this.enterpriseId
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tableHeight: '',
    };
  },
  created() {
    this.tableHeight = 600;
    listReferenceData().then(res=>{
      if(res.rows){
        this.referenceDataList=res.rows
      }
    });
    this.getList();
  },
  methods: {
    /** 查询物流列表 */
    getList() {
      this.loading = true;
      listLogistics(this.queryParams).then(response => {
        this.logisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        logisticsType: null,
        logisticsForm: null,
        logisticsProvider: null,
        vehicleOwnershipAndControl: null,
        fuelAndUnits: null,
        quantityPerYear: null,
        modeAndDescription: null,
        tonKmPerYear: null,
        referenceId: null,
        infoId: this.enterpriseId
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLogistics(id).then(response => {
        this.form = response.data;
        this.form.fuelAndUnits = parseInt(this.form.fuelAndUnits);
        this.form.modeAndDescription = parseInt(this.form.modeAndDescription);
        this.open = true;
        this.title = "修改物流";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLogistics(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addLogistics(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除物流编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLogistics(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有物流数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportLogistics(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style scoped lang="scss">
  .fuelUse {
    width: 100%;
    height: 100%;
  }
  .dialog-footer {
    text-align: right;
  }
  
  .el-table {
    height: 100%;
    width: 100%;
  }
</style>